(function () {
    var io = ppt.io;
    var CONFIG = {
        "1" : {
            COUNT : 2,
            TYPE : "1v1"
        },
        "2" : {
            COUNT : 4,
            TYPE : "2v2"
        }
    };

    function Waiting(data) {
        Waiting.super(this);

        this.ASSETS = {
            RES : ASSETS.MATCH_WAIT,
            TYPE : "simple"
        };

        //this.goBackEnabled = true;
        this.BG = null;
        this.ani = null;
        this.counter = null;
        this.content = null;
        this.players = {};
        this.data = data;
        this.mySeatId = null;

        this.init();
    }
    Laya.class(Waiting, "Scene.Waiting", Laya.Sprite);
    var _proto = Waiting.prototype;
    _proto.init = function () {
        this.size(GAME_CONFIG.WIDTH, Laya.stage.height);

        io.register({
            "pvp::enter" : this.userEnter,
            "pvp::startCd" : this.countDown,
            "game::exit" : this.exitGame,
            "game::ready" : this.gotoGame
        }, this);
    }
    _proto.userEnter = function (data) {
        this.players[data.seatId].update("enter", data);
    }
    _proto.exitGame = function (data) {
        if(data.code && data.code == 118){
            var config = {
                msg: data.msg,
                close: true,
                btn: {
                    btnCancel: {
                        skin: "cancel"
                    },
                    btnConfirm: {
                        skin: "confirm"
                    }
                }
            }
            ppt.director.popScene(new Alert.Public(config));
            return;
        }
        if(data.seatId == this.mySeatId){
            ppt.director.runScene(new Scene.Home);
        }else{
            this.players[data.seatId].update("exit");
        }
    }
    _proto.countDown = function () {
        //this.goBackEnabled = false;
        this.counter.count();
    }
    _proto.gotoGame = function (data) {
        console.log("Waiting Scene to GameScene");
        ppt.director.runScene(new Scene.Game(data));
    }

    _proto.onAdd = function () {
        var roomType = CONFIG[this.data.type].TYPE;
        var playerNum = CONFIG[this.data.type].COUNT;
        var playersData = {};

        for(var i in this.data.userList){
            playersData[this.data.userList[i].seatId] = this.data.userList[i];
        }

        // var filter = new Laya.BlurFilter();
        //     filter.strength = 5;

        var BG = utils.createSkeleton("res/public/public_bg");
            BG.pos(GAME_CONFIG.WIDTH / 2, Laya.stage.height / 2);
            BG.play(0, true);
            // BG.filters = [filter];

        var content = new ui.Waiting.WaitingBGUI();
            content.centerX = 0;
            content.centerY = 0;
            content.light.play(0, true);

        for(var i = 0; i < playerNum; i ++){
            var seatId = i + 1;
            var player = new Com.Waiting.Player(roomType, seatId);
            
            if(playersData[seatId]){
                player.update("enter", playersData[seatId]);
                if(playersData[seatId].userId == userId){
                    this.mySeatId = seatId;
                }
            }
            
            this.players[seatId] = player;
            content.addChild(player);
        }

        var ani = utils.createSkeleton("res/matching_waiting/vs");
            ani.pos(content.width / 2, 190);
            ani.once(Laya.Event.STOPPED, this, function () {
                this.ani.play("loop", true);
            });

        var counter = new Com.Waiting.Counter();

        content.addChildren(ani, counter);

        this.BG = BG;
        this.content = content;
        this.ani = ani;
        this.counter = counter;
        this.addChildren(BG, content);
    }
    _proto.onEnter = function () {
        ppt.header.enter("waiting", CONFIG[this.data.type].TYPE);

        this.ani.play("enter", false);

        for(var i in this.players){
            this.players[i].enter();
        }
    }
    _proto.onExit = function () {
        // if(this.goBackEnabled){
        //     io.emit({"cmd" : "game::exit"});
        // }

        this.destroy(true);

        io.unregister("pvp::enter", this.enter);
        io.unregister("pvp::startCd", this.countDown);
        io.unregister("game::exit", this.enter);
        io.unregister("game::ready", this.gotoGame);

        this.BG = null;
        this.ani = null;
        this.counter = null;
        this.content = null;
        this.players = null;
        this.data = null;
        this.mySeatId = null;
    }
    _proto.onResize = function (height) {
        this.height = height;
        
        this.BG.y = height / 2;
        this.content.centerY = 0;
    }
})();